// pages/teacher/homework-create/homework-create.js
Page({
  data: {
    formData: {
      title: '',
      courseId: 0,
      description: '',
      dueDate: '',
      totalPoints: 100,
      timeLimit: 60
    },
    courseList: [
      { id: 1, name: '微信小程序开发实战' },
      { id: 2, name: 'Python数据分析与挖掘' },
      { id: 3, name: 'UI/UX设计精品课' }
    ],
    questions: [],
    currentQuestion: {
      type: 'single_choice',
      question: '',
      options: ['', '', '', ''],
      correctAnswer: '',
      points: 10,
      explanation: ''
    },
    questionTypes: [
      { value: 'single_choice', label: '单选题' },
      { value: 'multiple_choice', label: '多选题' },
      { value: 'fill_blank', label: '填空题' }
    ],
    currentQuestionTypeIndex: 0,
    showQuestionModal: false
  },
  onLoad(options) {},
  // 表单输入
  onTitleInput(e) {
    this.setData({
      'formData.title': e.detail.value
    })
  },
  onDescInput(e) {
    this.setData({
      'formData.description': e.detail.value
    })
  },
  selectCourse(e) {
    this.setData({
      'formData.courseId': parseInt(e.detail.value)
    })
  },
  selectDueDate(e) {
    this.setData({
      'formData.dueDate': e.detail.value
    })
  },
  onPointsInput(e) {
    this.setData({
      'formData.totalPoints': parseInt(e.detail.value) || 100
    })
  },
  onTimeLimitInput(e) {
    this.setData({
      'formData.timeLimit': parseInt(e.detail.value) || 60
    })
  },
  // 题目管理
  showAddQuestion() {
    this.setData({
      showQuestionModal: true,
      currentQuestionTypeIndex: 0,
      currentQuestion: {
        type: 'single_choice',
        question: '',
        options: ['', '', '', ''],
        correctAnswer: '',
        points: 10,
        explanation: ''
      }
    })
  },
  hideQuestionModal() {
    this.setData({
      showQuestionModal: false
    })
  },
  selectQuestionType(e) {
    const index = parseInt(e.detail.value)
    const type = this.data.questionTypes[index].value
    this.setData({
      currentQuestionTypeIndex: index,
      'currentQuestion.type': type
    })
  },
  onQuestionInput(e) {
    this.setData({
      'currentQuestion.question': e.detail.value
    })
  },
  onOptionInput(e) {
    const index = e.currentTarget.dataset.index
    const value = e.detail.value
    const options = [...this.data.currentQuestion.options]
    options[index] = value
    this.setData({
      'currentQuestion.options': options
    })
  },
  onCorrectAnswerInput(e) {
    this.setData({
      'currentQuestion.correctAnswer': e.detail.value
    })
  },
  onQuestionPointsInput(e) {
    this.setData({
      'currentQuestion.points': parseInt(e.detail.value) || 10
    })
  },
  onExplanationInput(e) {
    this.setData({
      'currentQuestion.explanation': e.detail.value
    })
  },
  addQuestion() {
    const { currentQuestion, questions } = this.data
    
    // 验证
    if (!currentQuestion.question) {
      wx.showToast({
        title: '请输入题目',
        icon: 'none'
      })
      return
    }
    
    if (currentQuestion.type !== 'fill_blank') {
      const hasEmptyOption = currentQuestion.options.some(opt => !opt.trim())
      if (hasEmptyOption) {
        wx.showToast({
          title: '请填写所有选项',
          icon: 'none'
        })
        return
      }
    }
    
    if (!currentQuestion.correctAnswer) {
      wx.showToast({
        title: '请设置正确答案',
        icon: 'none'
      })
      return
    }
    
    questions.push({
      ...currentQuestion,
      id: Date.now()
    })
    
    this.setData({
      questions,
      showQuestionModal: false
    })
    
    wx.showToast({
      title: '题目添加成功',
      icon: 'success'
    })
  },
  deleteQuestion(e) {
    const id = e.currentTarget.dataset.id
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这道题目吗？',
      success: (res) => {
        if (res.confirm) {
          const questions = this.data.questions.filter(q => q.id !== id)
          this.setData({ questions })
        }
      }
    })
  },
  fromQuestionBank() {
    wx.navigateTo({
      url: '/pages/teacher/question-bank/question-bank?selectMode=true'
    })
  },
  // 提交作业
  submitHomework() {
    const { formData, questions } = this.data
    
    if (!formData.title) {
      wx.showToast({
        title: '请输入作业标题',
        icon: 'none'
      })
      return
    }
    
    if (formData.courseId === 0) {
      wx.showToast({
        title: '请选择课程',
        icon: 'none'
      })
      return
    }
    
    if (questions.length === 0) {
      wx.showToast({
        title: '请至少添加一道题目',
        icon: 'none'
      })
      return
    }
    
    wx.showLoading({ title: '发布中...' })
    
    // 模拟发布
    setTimeout(() => {
      wx.hideLoading()
      wx.showToast({
        title: '作业发布成功',
        icon: 'success'
      })
      
      setTimeout(() => {
        wx.navigateBack()
      }, 1500)
    }, 1500)
  },
  saveDraft() {
    wx.showToast({
      title: '草稿已保存',
      icon: 'success'
    })
  }
})

